package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiJuan;
import cn.com.api.entity.ApiT_coupon_act;
import cn.com.api.mapper.ApiJuanMapper;
import cn.com.api.mapper.ApiT_coupon_actMapper;
import cn.com.api.service.ApiT_coupon_actService;
import cn.com.util.*;
import cn.com.util.*;

@Service
public class ApiT_coupon_actServiceImpl implements ApiT_coupon_actService{
	
	@Autowired
	private ApiT_coupon_actMapper apiT_coupon_actMapper;
	@Autowired
	private ApiJuanMapper apiJuanMapper;
	
	@Override
	public List<ApiT_coupon_act> searchCouponActList(
			Map<String, Object> conditions, PageInfo pageInfo) {
		List<ApiT_coupon_act> list = apiT_coupon_actMapper.findByConditionsListPage(conditions, pageInfo);
		if(list!=null && list.size()>0){
			for(ApiT_coupon_act coupon : list){//页面只需要展示金额
				String invite = coupon.getInvite();
				String being_invited = coupon.getBeing_invited();
				Double invite_cash = 0.0;
				Double being_invited_cash = 0.0;
				if(!Util.isNull(invite)){
					String[] inviteArray = invite.split(",");
					if(inviteArray!=null && inviteArray.length>0){
						List<ApiJuan> list1 = new ArrayList<ApiJuan>();
						for(String id : inviteArray){
							if(!Util.isNull(id)){
								ApiJuan juan = apiJuanMapper.selectJuanByCid(Integer.valueOf(id));
								list1.add(juan);
								invite_cash = invite_cash + juan.getCashed();
							}
							
						}
					}
				}
				if(!Util.isNull(being_invited)){
					String[] being_invitedArray = being_invited.split(",");
					if(being_invitedArray!=null && being_invitedArray.length>0){
						List<ApiJuan> list2 =  new ArrayList<ApiJuan>();
						for(String id : being_invitedArray){
							if(!Util.isNull(id)){
								ApiJuan juan = apiJuanMapper.selectJuanByCid(Integer.valueOf(id));
								list2.add(juan);
								being_invited_cash = being_invited_cash + juan.getCashed();
							}
							
						}
					}
				}
				coupon.setInvite_cash(invite_cash);
				coupon.setBeing_invited_cash(being_invited_cash);
			}
		}
		return list;
	}

	@Override
	public ApiT_coupon_act searchCouponActById(String id) {
		ApiT_coupon_act coupon = null;
		if(!Util.isNull(id)){
			coupon = apiT_coupon_actMapper.searchCouponActById(id);
			if(coupon!=null){
				String invite = coupon.getInvite();
				String being_invited = coupon.getBeing_invited();
				List<ApiJuan> list1 = new ArrayList<ApiJuan>();
				List<ApiJuan> list2 = new ArrayList<ApiJuan>();
				if(!Util.isNull(invite)){
					String[] inviteArray = invite.split(",");
					if(inviteArray!=null && inviteArray.length>0){
						for(String cid : inviteArray){
							if(!Util.isNull(cid)){
								ApiJuan juan = apiJuanMapper.selectJuanByCid(Integer.valueOf(cid));
								list1.add(juan);
							}
							
						}
					}
					
				}
				if(!Util.isNull(being_invited)){
					String[] being_invitedArray = being_invited.split(",");
					if(being_invitedArray!=null && being_invitedArray.length>0){
						for(String cid : being_invitedArray){
							if(!Util.isNull(cid)){
								ApiJuan juan = apiJuanMapper.selectJuanByCid(Integer.valueOf(cid));
								list2.add(juan);
							}
							
						}
					}
					
				}
				coupon.setInviteList(list1);
				coupon.setBeing_invitedList(list2);
			}
		}
		
		return coupon;
	}

	@Override
	public void deleteCouponActById(String id) {
		apiT_coupon_actMapper.deleteCouponActById(id);
	}

	@Override
	public void addOrUpdateCouponAct(ApiT_coupon_act coupon_act) {
		Integer id = coupon_act.getId();
		if(id!=null){
			apiT_coupon_actMapper.updateT_coupon_act(coupon_act);
		}else{
			apiT_coupon_actMapper.insertT_coupon_act(coupon_act);
		}
	}
	
	/** 校验该活动在该城市是否添加过 */
	@Override
	public boolean checkExistCouponAct(ApiT_coupon_act coupon_act) {
		boolean flag = false;
		ApiT_coupon_act coupon_act1 = apiT_coupon_actMapper.checkExistCouponAct(coupon_act);
		if(coupon_act1==null){
			flag = false;
		}else{
			flag = true;
		}
		return flag;
	}
}
